package com.black.app.service.impl.store;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.store.edit.AppStoreAddressesEditReq;
import com.black.app.domain.store.AppStoreAddresses;
import com.black.app.domain.req.store.add.AppStoreAddressesAddReq;
import com.black.app.domain.req.store.query.AppStoreAddressesQueryReq;
import com.black.app.domain.resp.store.AppStoreAddressesResp;
import com.black.app.service.store.AppStoreAddressesService;
import com.black.app.mapper.store.AppStoreAddressesMapper;
import com.black.common.constant.CacheConstants;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author zhous
* @description 针对表【app_store_addresses】的数据库操作Service实现
* @createDate 2024-10-23 16:07:28
*/
@Service
public class AppStoreAddressesServiceImpl extends ServiceImpl<AppStoreAddressesMapper, AppStoreAddresses>
    implements AppStoreAddressesService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     * @param req
     * @return
     */
    @Override
    public PageInfo<AppStoreAddressesResp> paginationQuery(AppStoreAddressesQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * 新增地址
     * @param req
     * @return
     */
    @Override
    public boolean add(AppStoreAddressesAddReq req) {
        AppStoreAddresses newEntity = new AppStoreAddresses();
        // 对象拷贝
        BeanUtils.copyProperties(req, newEntity);
        boolean save = this.save(newEntity);
        if (save) {
            redisTemplate.opsForValue().set(CacheConstants.APP_STORE_ADDRESSES_KEY + newEntity.getAddressId(), newEntity);
        }
        return save;
    }

    /**
     * 修改地址
     * @param req
     * @return
     */
    @Override
    public boolean edit(AppStoreAddressesEditReq req) {
        // 根据主键查询
        AppStoreAddresses appStoreAddresses = this.getById(req.getAddressId());
        // 判断 查找不到数据
        if (Objects.isNull(appStoreAddresses)) {
            throw new RecordNotFoundException();
        }
        // 对象拷贝
        BeanUtils.copyProperties(req, appStoreAddresses);
        // 更新
        boolean res = this.updateById(appStoreAddresses);
        return res;
    }

    /**
     * 根据主键查询
     * @param addressId
     * @return
     */
    @Override
    public AppStoreAddressesResp findByAddressId(Long addressId) {
        if (Objects.isNull(addressId)) {
            throw new PrimaryException();
        }
        // 根据主键查询
        AppStoreAddresses appStoreAddresses = this.getById(addressId);
        if (Objects.isNull(appStoreAddresses)) {
            throw new RecordNotFoundException();
        }
        AppStoreAddressesResp resp = new AppStoreAddressesResp();
        BeanUtils.copyProperties(appStoreAddresses, resp);

        return resp;
    }

    /**
     * 根据主键删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteById(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }
        for (Long addressId : ids) {
            AppStoreAddresses appStoreAddresses = this.getById(addressId);
            if (Objects.nonNull(appStoreAddresses)) {
                this.removeById(addressId);
            }
        }
        return true;
    }

}




